package edu.taru.view;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import edu.taru.pojo.User;
import edu.taru.service.AuthorityServices;
import edu.taru.service.Services;

import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UpdatePasswordView extends JFrame{
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	public UpdatePasswordView() {
		setTitle("修改密码");
		setVisible(true);
		setSize(415, 266);
		getContentPane().setLayout(null);
		
		JLabel label = new JLabel("旧密码");
		label.setBounds(116, 73, 54, 15);
		getContentPane().add(label);
		
		JLabel label_1 = new JLabel("新密码");
		label_1.setBounds(116, 98, 54, 15);
		getContentPane().add(label_1);
		
		JButton button = new JButton("提交");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Services services = new Services();
				User user = services.getLogined();
				System.out.println(user.getPassword());
				System.out.println(user.getUsername());
				
				//获取旧密码
				String oldPassword =new String( passwordField.getPassword());
				//获取新密码
				String newPassword = new String(passwordField_1.getPassword());
				AuthorityServices authorityServices = new AuthorityServices();
				try {
					
					authorityServices.updatePassword(user, newPassword, oldPassword);
					JOptionPane.showMessageDialog(UpdatePasswordView.this, "修改密码成功");
					UpdatePasswordView.this.dispose();
				}catch(Exception ex) {
					JOptionPane.showMessageDialog(UpdatePasswordView.this,"修改密码失败");
					
				}
				
				
			}
		});
		button.setBounds(161, 163, 93, 23);
		getContentPane().add(button);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(180, 70, 136, 21);
		getContentPane().add(passwordField);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(180, 95, 136, 21);
		getContentPane().add(passwordField_1);
	}
}
